package com.fesine.crm.service.impl;

import com.fesine.commons.excel.ObjectExcelRead;
import com.fesine.commons.excel.PageData;
import com.fesine.commons.util.CryptographyUtil;
import com.fesine.crm.po.EmployeeInfoPo;
import com.fesine.crm.service.CrmFileService;
import com.fesine.crm.service.EmployeeInfoService;
import com.fesine.dao.model.QueryResult;
import com.fesine.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/10/11 21:17
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/10/11 21:17
 */
@Service("employeeInfoService")
public class EmployeeInfoServiceImpl extends BaseServiceImpl<EmployeeInfoPo> implements
        EmployeeInfoService,CrmFileService {
    private static final Logger logger = LoggerFactory.getLogger(AttendanceInfoServiceImpl.class);

    @Value("${user.password}")
    private String password;

    @Override
    public int save(EmployeeInfoPo po) {
        Date date = new Date();
        //录入公司号字母大写
        po.setPacteraNo(po.getPacteraNo().toUpperCase());
        //使用md5加密，不可逆
        po.setPassword(CryptographyUtil.md5(po.getPassword(), "fesine"));
        po.setCreateTime(date);
        po.setUpdateTime(date);
        return daoService.insert(po);
    }

    @Override
    public int update(EmployeeInfoPo po) {
        Date date = new Date();
        setUpperPacteraNo(po);

        //使用md5加密，不可逆
        if (StringUtils.isNotBlank(po.getPassword())) {
            po.setPassword(CryptographyUtil.md5(po.getPassword(), "fesine"));
        }
        po.setUpdateTime(date);
        return daoService.update(po);
    }

    private void setUpperPacteraNo(EmployeeInfoPo po) {
        //录入公司号字母大写
        if (StringUtils.isNotBlank(po.getPacteraNo())) {
            po.setPacteraNo(po.getPacteraNo().toUpperCase());
        }
    }

    @Override
    public int delete(EmployeeInfoPo po) {
        setUpperPacteraNo(po);
        return daoService.delete(po);
    }

    @Override
    public QueryResult<EmployeeInfoPo> listPage(EmployeeInfoPo po, int page, int limit) {
        setUpperPacteraNo(po);
        return daoService.selectQueryResult(po, page, limit);
    }

    @Override
    public EmployeeInfoPo get(EmployeeInfoPo po) {
        setUpperPacteraNo(po);
        return daoService.selectOne(po);
    }

    @Override
    public void importFile(File file) throws Exception {
        //从第1行开始读取数据
        List<Object> ol = ObjectExcelRead.readExcel(file.getName(), file, 0, 1, 0);
        EmployeeInfoPo empPo;
        for (Object o : ol) {
            PageData pg = (PageData) o;
            String name = (String) pg.get("var0");
            if (StringUtils.isBlank(name)) {
                continue;
            }
            logger.info("----->登记数据库 {}", name);
            empPo = new EmployeeInfoPo();
            empPo.setName(name);
            if ("女".equals(((String) pg.get("var1")).trim())) {
                empPo.setSex(0);
            } else if ("男".equals(((String) pg.get("var1")).trim())) {
                empPo.setSex(1);
            } else {
                empPo.setSex(2);
            }
            empPo.setPacteraNo((String) pg.get("var2"));
            empPo.setCellphone((String) pg.get("var3"));
            empPo.setJobNo((String) pg.get("var4"));
            empPo.setPassword(CryptographyUtil.md5(password, "fesine"));
            empPo.setCreateTime(new Date());
            empPo.setUpdateTime(new Date());
            daoService.insert(empPo);
        }
    }

    @Override
    public List<EmployeeInfoPo> listAll(EmployeeInfoPo po) {
        setUpperPacteraNo(po);
        return daoService.selectList(po);
    }

}
